package com.shanghaionstar.smart.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.weight.CommonWebView;
import com.shanghaionstar.R;

public class SmartProtocolDialog extends Dialog {
    private ViewHolder holder;
    private Context context;

    private IProtocolClickListener listener;

    public SmartProtocolDialog(Context context) {
        this(context, R.style.Theme_Dialog_NoTitleBar, null);
    }

    public SmartProtocolDialog(Context context, IProtocolClickListener listener) {
        this(context, R.style.Theme_Dialog_NoTitleBar, listener);
    }

    public SmartProtocolDialog(Context context, int theme, IProtocolClickListener listener) {
        super(context, theme);
        holder = new ViewHolder();
        this.context = context;
        View view = View.inflate(context, R.layout.smart_protocol_dia, null);
        holder.init(view);
        setContentView(view);
        setGravity();
        setWidthHeight();
        initWebView();
        this.listener = listener;
        setCanceledOnTouchOutside(false);
    }

    protected void setWebViewClient() {
        holder.protocolWv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                holder.progressView.setVisibility(ProgressBar.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                holder.progressView.setVisibility(ProgressBar.GONE);
            }
        });
    }


    private void initWebView() {
        setWebViewClient();
    }

    public void loadUrl(String url) {
        holder.protocolWv.loadUrl(url);
    }

    /**
     * 居中
     */
    private void setGravity() {
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
    }

    /**
     * 横向全屏
     */
    private void setWidthHeight() {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth() * 4 / 5;
        int height = wm.getDefaultDisplay().getHeight() *2/ 3;
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, height);
        holder.protocolRl.setLayoutParams(lp);
    }


    public class ViewHolder {
        RelativeLayout protocolRl;
        ProgressBar progressView;
        CommonWebView protocolWv;
        TextView refuseTv, agreeTv;

        public void init(View view) {
            protocolRl = (RelativeLayout) view.findViewById(R.id.smart_pro_rl);
            progressView = (ProgressBar) view.findViewById(R.id.loading_pb);
            protocolWv = (CommonWebView) view.findViewById(R.id.protocol_wv);
            refuseTv = (TextView) view.findViewById(R.id.refuse_tv);
            agreeTv = (TextView) view.findViewById(R.id.agree_tv);

            refuseTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(listener != null)
                        listener.onClick(refuseTv);
                }
            });

            agreeTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(listener != null)
                        listener.onClick(agreeTv);
                }
            });
        }
    }

    public IProtocolClickListener getListener() {
        return listener;
    }

    public void setListener(IProtocolClickListener listener) {
        this.listener = listener;
    }

    public interface IProtocolClickListener{
        void onClick(View view);
    }
}
